package org.example.design.test;

public class HelloWorldThread {
    public static void main(String[] args) {
        // 1. 输出HelloWorld
        System.out.println("HelloWorld");

        // 2. 获取当前JVM中所有活跃线程（线程名 -> 堆栈信息的映射）
        ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
        // 遍历获取根线程组（避免遗漏子线程组）
        while (rootGroup.getParent() != null) {
            rootGroup = rootGroup.getParent();
        }

        // 统计并输出所有线程
        Thread[] threads = new Thread[rootGroup.activeCount() * 2]; // 扩容避免数组长度不足
        int threadCount = rootGroup.enumerate(threads, true);

        System.out.println("\n当前JVM活跃线程共 " + threadCount + " 个：");
        for (int i = 0; i < threadCount; i++) {
            Thread thread = threads[i];
            // 输出线程名、状态、是否为守护线程
            System.out.printf("线程名：%s | 状态：%s | 守护线程：%b%n",
                    thread.getName(), thread.getState(), thread.isDaemon());
        }
    }
}
